package vno.sci.mapEditor
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.ui.Keyboard;
	import flash.ui.KeyboardType;
	import flash.ui.Mouse;
	import flash.utils.ByteArray;
	
	import hr.binaria.asx3m.Asx3mer;
	
	import org.aswing.AsWingManager;
	import org.aswing.BorderLayout;
	import org.aswing.DefaultBoundedRangeModel;
	import org.aswing.JButton;
	import org.aswing.JFrame;
	import org.aswing.JLabel;
	import org.aswing.JPanel;
	import org.aswing.JPopup;
	import org.aswing.JScrollBar;
	import org.aswing.JSlider;
	import org.aswing.JTextArea;
	import org.aswing.SoftBox;
	import org.aswing.SoftBoxLayout;
	
	import vno.sci.mapEditor.Drawer;
	import vno.sci.window.SimpleMessageBox;
	
	public class MapEditor extends Sprite
	{
		//请输入你的文件地址
		[Embed(source="C:/Users/scinart/Pictures/map.jpg")]
//		[Embed(source="src/vno/sci/mapEditor/map-example.jpg")]
		
		private var _Ball:Class;
		private var _map:Bitmap = new _Ball();
//		private var map:Bitmap = new Bitmap(new BitmapData(3000,3000,false,0));
		
		private var _mapWidth:int;
		private var _mapHeight:int;
		private var _blockWidth:int;
		private var _blockHeight:int;
		//创建界限范围模型，默认值10，最小值1，最大值1000
		private var _weight:DefaultBoundedRangeModel;
		
		private var _drawer:Drawer;
		private var _popup:JPopup;
		/**
		 * 使用方法，
		 * var a:MapEditor = new MapEditor(stage,20,20);
		 * 在MapEditor里输入你的图片地址
		 * Save 和 Load是机器读的，可逆，快捷键为S, L
		 * Export是输入指定格式，不可逆，快捷键为E
		 * Circle和Rectangle 快捷键为 C, R 
		 * 撤消，快捷键为U或Z
		 * 删除，Ctrl+鼠标点击
		 * 方向键或鼠标渡轮移动。Alt加速，Shift减速，Ctrl为横向滚动
		 * @param stage
		 * 
		 */		
		public function MapEditor(stage_:Stage, theBlockWeight_:int = 20, theBlockHeight_:int = 20)
		{	
			AsWingManager.initAsStandard(this);
			
			_mapWidth = _map.width;
			_mapHeight = _map.height;
			_blockWidth = theBlockWeight_;
			_blockHeight = theBlockHeight_;
			
			_weight =  new  DefaultBoundedRangeModel(1, 0, 0, Drawer.enumColor.length-1);
			_drawer= new Drawer(_mapWidth, _mapHeight, _blockWidth, _blockHeight,_map, _weight);
			

			stage_.addEventListener(KeyboardEvent.KEY_UP,onKeyUp);
			stage_.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);

			this.addChild(_drawer);
			this.addUi();
			stage_.addChild(this);
		}
		
		private function onKeyUp(e:KeyboardEvent):void
		{
			if(e.keyCode == Keyboard.Z)
			{
				_drawer.undo();
			}
			if(e.keyCode == Keyboard.U)
			{
				_drawer.undo();
			}
			if(e.keyCode == Keyboard.S)
			{
				save();
			}
			if(e.keyCode == Keyboard.L)
			{
				load();
			}
			if(e.keyCode == Keyboard.E)
			{
				export();
			}
			if(e.keyCode == Keyboard.C)
			{
				_drawer.shape = "Circle";
			}
			if(e.keyCode == Keyboard.R)
			{
				_drawer.shape = "Rectangle";	
			}
		}
		private function onKeyDown(e:KeyboardEvent):void
		{
			var factor:int = 4;
			if(e.shiftKey)
			{
				factor /= 2;
			}
			if(e.altKey)
			{
				factor *= 3;
			}
			if(e.keyCode == Keyboard.UP)
			{
				_drawer.y += _blockWidth*factor;
			}
			if(e.keyCode == Keyboard.DOWN)
			{			
				_drawer.y -= _blockWidth*factor;
			}
			if(e.keyCode == Keyboard.LEFT)
			{
				_drawer.x += _blockWidth*factor;
			}
			if(e.keyCode == Keyboard.RIGHT)
			{
				_drawer.x -= _blockWidth*factor;
			}
		}
		private function addUi():void
		{
			var jsave:JButton = new JButton("Save(S)");
			jsave.addEventListener(MouseEvent.MOUSE_UP,function (e:Event):void{save();});
			var jload:JButton = new JButton("Load(L)");
			jload.addEventListener(MouseEvent.MOUSE_UP,function (e:MouseEvent):void{load();});
			var jexport:JButton = new JButton("Load老版本(E)");
			jexport.addEventListener(MouseEvent.MOUSE_UP, function (e:MouseEvent):void{export();});
			var jsaveold:JButton = new JButton("Save老版本()");
			jsaveold.addEventListener(MouseEvent.MOUSE_UP, function (e:MouseEvent):void{export();});
			var circle:JButton = new JButton("Circle(C)");
			circle.addEventListener(MouseEvent.MOUSE_UP, function (e:MouseEvent):void{_drawer.shape = "Circle";});
			var rectangle:JButton = new JButton("Rectangle(R)");
			rectangle.addEventListener(MouseEvent.MOUSE_UP, function (e:MouseEvent):void{_drawer.shape = "Rectangle";});
			var undo:JButton = new JButton("Undo(Z,U)");
			undo.addEventListener(MouseEvent.MOUSE_UP, function (e:MouseEvent):void{_drawer.undo();});
			var remove:JButton = new JButton("Ctrl键删除");
			remove.setEnabled(false);
			var directionKey:JButton = new JButton("方向键移动");
			directionKey.setEnabled(false);
			var help:JButton = new JButton("帮助");
			var increaseWeight:JButton = new JButton("增加权值");
			var decreaseWeight:JButton = new JButton("减少权值");
			
			
			//创建滑动条
			var slider:JSlider =  new JSlider(JSlider.HORIZONTAL);
			slider.setModel(_weight);
			slider.setShowValueTip(true);
			slider.setPaintTicks(true);
			slider.setMajorTickSpacing(200);
			slider.setMinorTickSpacing(20);
			slider.setPaintTrack(true);
			//创建滚动条
			var scrollbar:JScrollBar  =  new JScrollBar(JScrollBar.HORIZONTAL);
			scrollbar.setModel(_weight);
			
			var onHelp:Function = function (e:MouseEvent):void
			{
				var frame:JFrame = new JFrame(stage);
				frame.setSizeWH(500, 300);
				frame.setY(20);
				frame.setTitle("帮助");
				var pane:JTextArea = new JTextArea("* 使用方法，在MapEditor里输入你的图片地址\n" +
					"* Save 和 Load是机器读的，使用自定义格式序列化，使用16位整数，与之前版本不兼容，可逆，快捷键为S, L\n* " + //Export是输入指定格式，不可逆，快捷键为E\n" +
					"* Circle和Rectangle 快捷键为 C, R\n* 撤消，快捷键为U或Z\n* 删除，Ctrl+鼠标点击\n" +
					"* 方向键或鼠标渡轮移动。Alt加速，Shift减速，Ctrl为横向滚动");
				frame.getContentPane().append(pane);
				frame.show();
			}
			help.addEventListener(MouseEvent.CLICK,onHelp);
			var panel:JPanel = new JPanel(new SoftBoxLayout());
			panel.appendAll(jsave, jload,jexport,jsaveold, circle, rectangle, undo, help, scrollbar, slider);
			
						
			_popup = new JPopup(this);
			
			_popup.setLayout(new BorderLayout());
			_popup.setSizeWH(_mapWidth, _mapHeight);
			
			_popup.addChild(_drawer);
			_popup.append(panel, BorderLayout.NORTH);
			_popup.show();
			this.addChild(_popup);
		}
		/**
		 * 载入原来的文件，就像刚刚编辑过一样
		 */		
		public function load():void
		{
			var fileRef:FileReference = new FileReference();
			fileRef.browse();
			fileRef.addEventListener(Event.SELECT, onFileSelected);
			
			function onFileSelected(e:Event):void 
			{
				fileRef.addEventListener(Event.COMPLETE, onFileLoaded);
				fileRef.load();
			}
			function onFileLoaded(e:Event):void 
			{
				var ba:ByteArray = e.target.data;
				_drawer.binaryLoad(ba);	
				_popup.removeChild(_drawer);
				_popup.addChildAt(_drawer,0);
			}
		}
		/**
		 * 将当前画图序列化。可以重新load回来 
		 */		
		public function save():void
		{
			var fileRef:FileReference = new FileReference();
			var ba:ByteArray;
			ba = _drawer.binarySave();
			fileRef.save(ba);
		}
		/**
		 * load老版本
		 */		
		private function export():void
		{
			//trace (Asx3mer.instance.toXML(Drawer.toArray(drawer.binarySave())));
			var fileRef:FileReference = new FileReference();
			fileRef.browse();
			fileRef.addEventListener(Event.SELECT, onFileSelected);
			
			function onFileSelected(e:Event):void 
			{
				fileRef.addEventListener(Event.COMPLETE, onFileLoaded);
				fileRef.load();
			}
			function onFileLoaded(e:Event):void 
			{
				var ba:ByteArray = e.target.data;
				_drawer.load(ba);	
				_popup.removeChild(_drawer);
				_popup.addChildAt(_drawer,0);
			}
		}
		private function saveXML():void
		{
			var fileRef:FileReference = new FileReference();
			var ba:ByteArray;
			ba = _drawer.save();
			fileRef.save(ba);
		}
		/**
		 * 生成二维int数组，行主序 
		 * @return 二维数组 
		 * 
		 */		
		public function toArray():Vector.<Vector.<int>>
		{
			return Drawer.toArray(_drawer.save());
		}
		
		public static function mapToArray(ba:ByteArray):Vector.<Vector.<int>>
		{
			return Drawer.toArray(ba);
		}
	}
}